package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserExchange;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserExchangeDaoImpl extends TempDataObjectDao<UserExchange> {

    public Map<String, UserExchange> getUserExchangeMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String,UserExchange> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_s_user_exchange where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserExchange data = parse(rs);
                dataMap.put(data.getExchangeKey(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserExchange data) {
        PreparedStatement call = null;
        String sql = "insert into t_s_user_exchange(userId,exchangeId,exchangeKey,useTime) values (?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getExchangeId());
            call.setString(i++, data.getExchangeKey());
            call.setLong(i++, data.getUseTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserExchange data) {
        PreparedStatement call = null;
        String sql = "update t_s_user_exchange set useTime=? where userId=? and exchangeKey=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUseTime());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getExchangeKey());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserExchange parseData(ResultSet rs) throws SQLException {
        UserExchange data = new UserExchange();
        data.setUserId(rs.getLong("userId"));
        data.setExchangeId(rs.getInt("exchangeId"));
        data.setExchangeKey(rs.getString("exchangeKey"));
        data.setUseTime(rs.getLong("useTime"));
        return data;
    }
}
